|
RAIS
3.2
|
00001 using System; 00002 using System.Collections.Generic; 00003 using System.Linq; 00004 using System.Text; 00005 using System.Data.SqlClient; 00006 using System.Xml; 00007 using RAIS.DataAccessLayer; 00008 namespace FillCustomQueryTableCL 00009 { 00010 public class Query 00011 { 00012 #region Fields 00013 public static readonly string GetQueryTypes = 00014 "SELECT [PK Query Type ID], " + 00015 " [Query Type Visible Name] " + 00016 "FROM [Query Type]"; 00017 protected static Dictionary<string, int> QueryTypes = new Dictionary<string, int>(); 00018 protected static SqlConnection sqlConnection; 00019 string name; 00020 int type; 00021 string text; 00022 protected List<Parameter> Parameters = new List<Parameter>(); 00023 #endregion 00024 00025 #region Properties 00026 public int Type 00027 { 00028 get { return type; } 00029 set { type = value; } 00030 } 00031 00032 public string Name 00033 { 00034 get { return name; } 00035 set { name = value; } 00036 } 00037 00038 public string Text 00039 { 00040 get { return text; } 00041 set { text = value; } 00042 } 00043 #endregion 00044 00045 public static void InitQuery(string connectionString) 00046 { 00047 if (QueryTypes.Count == 0) 00048 { 00049 using (sqlConnection = new SqlConnection(connectionString)) 00050 { 00051 sqlConnection.Open(); 00052 using (SqlCommand sqlCommand = new SqlCommand("", sqlConnection)) 00053 { 00054 sqlCommand.CommandText = GetQueryTypes; 00055 using (SqlDataReader reader = sqlCommand.ExecuteReader()) 00056 { 00057 while (reader.Read()) 00058 QueryTypes.Add((string)reader["Query Type Visible Name"], (int)reader["PK Query Type ID"]); 00059 } 00060 } 00061 } 00062 } 00063 } 00064 00065 protected string GetTypeFromSQLType(string typeName) 00066 { 00067 string ret; 00068 switch (typeName) 00069 { 00070 case "int": 00071 ret = "Integer"; 00072 break; 00073 case "datetime": 00074 ret = "Date"; 00075 break; 00076 case "nvarchar": 00077 ret = "Text"; 00078 break; 00079 case "float": 00080 ret = "Rational"; 00081 break; 00082 case "xml": 00083 ret = "Xml"; 00084 break; 00085 default: 00086 ret = "Unknown"; 00087 break; 00088 } 00089 return ret; 00090 } 00091 00092 public static Dictionary<string, Query> Create(string connectionString, string fileName) 00093 { 00094 Dictionary<string, Query> dic = new Dictionary<string, Query>(); 00095 00096 XmlDocument doc = new XmlDocument(); 00097 doc.Load(fileName); 00098 XmlNodeList nodes = doc.GetElementsByTagName("Query"); 00099 using (sqlConnection = new SqlConnection(connectionString)) 00100 { 00101 sqlConnection.Open(); 00102 foreach (XmlNode node in nodes) 00103 { 00104 string type = node.Attributes["Type"].InnerText; 00105 string name = node.Attributes["Name"].InnerText; 00106 switch (type) 00107 { 00108 case "Consistency Check": 00109 dic.Add(name, new ConsistencyCheckQuery(name)); 00110 break; 00111 case "Entry Filter": 00112 dic.Add(name, new EntryFilterQuery(name)); 00113 break; 00114 case "Preselection List": 00115 dic.Add(name, new PreselectionListQuery(name)); 00116 break; 00117 case "Query": 00118 dic.Add(name, new QueryQuery(name)); 00119 break; 00120 case "Statistics": 00121 dic.Add(name, new StatisticsQuery(name)); 00122 break; 00123 case "Preselection Filter": 00124 dic.Add(name, new PreselectionFilterQuery(name)); 00125 break; 00126 case "RAN Autogeneration": 00127 dic.Add(name, new RANSP(name)); 00128 break; 00129 default: 00130 dic.Add(name, new OtherQuery(name)); 00131 break; 00132 } 00133 } 00134 } 00135 return dic; 00136 } 00137 00138 //public Query() { } 00139 protected Query(string name, string type) 00140 { 00141 this.name = name; 00142 this.type = QueryTypes[type]; 00143 StringBuilder sb = new StringBuilder(); 00144 using (SqlCommand sqlCommand = new SqlCommand("", sqlConnection)) 00145 { 00146 sqlCommand.CommandText = "sp_helptext '" + this.name + "'"; 00147 using (SqlDataReader reader = sqlCommand.ExecuteReader()) 00148 { 00149 while (reader.Read()) 00150 sb.AppendLine((string)reader[0]); 00151 } 00152 } 00153 sb = RemoveComments(sb); 00154 sb.Replace("
", " "); 00155 sb.Replace("	", " "); 00156 sb.Replace("\r\n\r\n", "\r\n"); 00157 text = GetQuery(sb.ToString()); 00158 } 00159 00160 protected virtual void PreСorrectParameters(SqlDataReader reder) 00161 { } 00162 00163 protected virtual void СorrectParameters() 00164 { } 00165 00166 #region Public methods 00167 00168 public void WriteToXML(XmlWriter query, XmlWriter parameters) 00169 { 00170 foreach (Parameter param in Parameters) 00171 param.WriteToXML(parameters); 00172 query.WriteStartElement("Query"); 00173 query.WriteAttributeString("QueryName", name); 00174 query.WriteAttributeString("QueryText", text); 00175 query.WriteAttributeString("QueryType", type.ToString()); 00176 query.WriteEndElement(); 00177 } 00178 #endregion 00179 00180 #region Private methods 00181 protected virtual string GetQuery(string inputQueru) 00182 { 00183 string str = inputQueru.Substring(inputQueru.IndexOf("begin", StringComparison.CurrentCultureIgnoreCase)); 00184 int startIndex = str.IndexOf("select", StringComparison.CurrentCultureIgnoreCase); 00185 int endIndex = str.IndexOf("return", startIndex, StringComparison.CurrentCultureIgnoreCase); 00186 00187 str = str.Substring(startIndex, endIndex - startIndex); 00188 return str; 00189 } 00190 00191 private StringBuilder RemoveComments(StringBuilder str) 00192 { 00193 StringBuilder ret = new StringBuilder(); 00194 bool inComments = false; 00195 bool inLineComment = false; 00196 for (int i = 0; i < str.Length; i++) 00197 { 00198 if (!inLineComment && !inComments && str[i] == '/' && str[i + 1] == '*') 00199 inComments = true; 00200 if (!inLineComment && !inComments && str[i] == '-' && str[i + 1] == '-') 00201 inLineComment = true; 00202 00203 if (!inComments && !inLineComment) 00204 ret.Append(str[i]); 00205 00206 if (inLineComment && i - 1 >= 0 && str[i] == '\n' && str[i - 1] == '\r') 00207 inLineComment = false; 00208 if (inComments && i - 1 >= 0 && str[i] == '/' && str[i - 1] == '*') 00209 inComments = false; 00210 } 00211 return ret; 00212 } 00213 00214 private int FindParamByName(string paramName) 00215 { 00216 for (int i=0;i<Parameters.Count;i++) 00217 if (Parameters[i].Name == paramName) 00218 return i; 00219 return -1; 00220 } 00221 #endregion 00222 } 00223 }